package br.brpv.silvestrin.vendas.manager;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Layout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import br.brpv.silvestrin.vendas.R;
import br.brpv.silvestrin.vendas.domain.*;
import br.brpv.silvestrin.vendas.listeners.ClientParamsDefaultQueryListener;
import br.brpv.silvestrin.vendas.listeners.ClientsQueryListener;
import br.brpv.silvestrin.vendas.listeners.EstablishmentQueryListener;
import br.brpv.silvestrin.vendas.util.ShowMessage;
import br.brpv.silvestrin.vendas.webservice.ServerConnection;
import br.brpv.silvestrin.vendas.webservice.ServerDataCacheService;

import java.util.ArrayList;


public class ClientManager implements ClientsQueryListener, EstablishmentQueryListener, ClientParamsDefaultQueryListener {
    private Activity activity;
    private ClientManagerInterface clientManagerInterface;
    private ClientManagerParamsInterface clientManagerParamsInterface;

    private int codSelectedCompany;
    private Client selectedClient;
    private ServerDataCacheService mServerDataCacheBinder;

    public ClientManager(Activity activity, ClientManagerInterface clientManagerInterface,
                         ClientManagerParamsInterface clientManagerParamsInterface) {
        this.activity = activity;
        this.clientManagerInterface = clientManagerInterface;
        this.clientManagerParamsInterface = clientManagerParamsInterface;
    }

    public void getClientList(){
        ArrayList<Client> clientsList = null;

        if(mServerDataCacheBinder != null) clientsList = mServerDataCacheBinder.getClientList(codSelectedCompany);
        if(clientsList == null || clientsList.size() == 0) ServerConnection.getClients(codSelectedCompany, this);
        else clientManagerInterface.onClientsListReturn(clientsList);
    }

    public void getEstablishmentsList(Client client){
        selectedClient = client;
        ArrayList<Establishment> establishmentList = null;

        if(mServerDataCacheBinder != null){
            establishmentList = mServerDataCacheBinder.getEstablishmentList(codSelectedCompany,
                    Integer.parseInt(client.getClientId()));
        }
        if(establishmentList == null || establishmentList.size() == 0) {
            ServerConnection.getEstablishments(codSelectedCompany, client.getClientId(), this);
        } else clientManagerInterface.onEstablishmentsListReturn(establishmentList);

    }

    public void getEstablishmentDefault(Client client){
        selectedClient = client;
        Establishment establishment = null;

        if(mServerDataCacheBinder != null){
            establishment = mServerDataCacheBinder.getEstablishmentDefault(codSelectedCompany,
                    Integer.parseInt(client.getClientId()));
        }
        if(establishment == null) {
            ServerConnection.getDefaultEstablishment(codSelectedCompany, client.getClientId(), this);
        } else clientManagerInterface.onDefaultEstablishmentReturn(establishment);
    }

    public void getClientDefaultParams(String clientId, String establishmentId){
        ServerConnection.getClientDefaultParams(codSelectedCompany, clientId,
                establishmentId, this);

    }

    public void checkPaymentLate(Establishment establishment){
        ServerConnection.getClientTitcr(codSelectedCompany,
                establishment.getClientId(), establishment.getEstablishmentId(), this);
    }

    public void getClientPlacas(String clientId){
        ServerConnection.getClientPlacas(clientId, this);
    }

    public void getClientAddress(String clientId){
        ServerConnection.getClientAddress(clientId, this);
    }

    public void setClientPlaca(String clientId, String placa) {
        System.out.println("Enviando placa: " + placa);
        ServerConnection.setClientPlaca(clientId, placa, this);
    }

    public void setClientAddress(String clientId, String address) {
        System.out.println("Enviando endereco: " + address);

        ServerConnection.setClientAddress(clientId, address, this);
    }



    public void setmServerDataCacheBinder(ServerDataCacheService mServerDataCacheBinder) {
        this.mServerDataCacheBinder = mServerDataCacheBinder;
    }

    public void setCodSelectedCompany(int codSelectedCompany) {
        this.codSelectedCompany = codSelectedCompany;
    }

    @Override
    public void OnWebServiceClientsReturn(ArrayList<Client> clientsList) {
        if(mServerDataCacheBinder != null) mServerDataCacheBinder.addClientListToCompany(codSelectedCompany, clientsList);
        clientManagerInterface.onClientsListReturn(clientsList);
    }

    @Override
    public void failedOnRetrieveClients() {
        ShowMessage.alertDialog2(activity, R.string.clientslist_failonretrieveclients, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case DialogInterface.BUTTON_NEGATIVE:
                        activity.finish();
                        break;
                    case DialogInterface.BUTTON_POSITIVE:
                        ServerConnection.getClients(codSelectedCompany, ClientManager.this);
                        break;
                }
            }
        });
    }

    @Override
    public void OnWebServiceClientPlacasReturn(ArrayList<Placa> placas) {
        System.out.println("Placas Return " + placas);
        clientManagerParamsInterface.onClientPlacasReturn(placas);
    }

    @Override
    public void failedOnRetrieveClientPlacas() {
        System.out.println("Placas Fail");
    }

    @Override
    public void OnWebServiceClientAddressReturn(ArrayList<Address> addresses) {
        System.out.println("Address Return " + addresses);
        clientManagerParamsInterface.onClientAddressReturn(addresses);
    }

    @Override
    public void failedOnRetrieveClientAddress() {
        System.out.println("Address Fail");
    }

    @Override
    public Context getActivity() {
        return activity;
    }

    @Override
    public void OnWebServiceSetClientPlacaReturn(String resultString) {
        System.out.println("Set Placas " + resultString);
        clientManagerParamsInterface.onClientSetPlacaReturn(resultString);
    }

    @Override
    public void failedOnSetClientPlaca() {
        System.out.println("Set Placas Fail");

    }

    @Override
    public void OnWebServiceSetClientAddressReturn(String resultString) {
        System.out.println("Set Address " + resultString);
        clientManagerParamsInterface.onClientSetAddressReturn(resultString);
    }

    @Override
    public void failedOnSetClientAddress() {
        System.out.println("Set Address Fail");

    }

    @Override
    public void OnWebServiceEstablishmentReturn(ArrayList<Establishment> estabs) {
        if(mServerDataCacheBinder != null){
            mServerDataCacheBinder.addEstablishmentToClient(codSelectedCompany,
                    Integer.parseInt(selectedClient.getClientId()), estabs);
        }
        clientManagerInterface.onEstablishmentsListReturn(estabs);
    }

    @Override
    public void failedOnRetrieveEstablishment() {
        ShowMessage.alertDialog2(getActivity(), R.string.clientslist_failonretrieveestablishment, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch(which) {
                    case DialogInterface.BUTTON_NEGATIVE:
                        //activity.finish();
                        break;
                    case DialogInterface.BUTTON_POSITIVE:
                        ServerConnection.getEstablishments(codSelectedCompany,
                                selectedClient.getClientId(),
                                ClientManager.this);
                        break;
                }
            }
        });
    }

    @Override
    public void OnWebServiceDefaultEstablishmentReturn(Establishment estab) {
        if(mServerDataCacheBinder != null){
            mServerDataCacheBinder.addEstablishmentDefaultToClient(codSelectedCompany,
                    Integer.parseInt(selectedClient.getClientId()), estab);
        }
        clientManagerInterface.onDefaultEstablishmentReturn(estab);
    }

    @Override
    public void failedOnRetrieveDefaultEstablishment() {
    }

    @Override
    public void failedOnEstabDefaultParams() {

    }

    @Override
    public void OnWebServiceEstabDefaultParamsReturn(EstabParamsDefault estabParamsDefault) {
        if(clientManagerInterface != null) clientManagerInterface.onEstablishmentParamsReturn(estabParamsDefault);
    }

    @Override
    public void OnWebServicePaymentLateReturn(ArrayList<PaymentLate> paymentLateArrayList) {
        if(!paymentLateArrayList.isEmpty()){
            Dialog dialog = new Dialog(activity);

            dialog.setOwnerActivity(activity);
            dialog.setContentView(R.layout.payment_late_dialog);
            dialog.setCanceledOnTouchOutside(true);
            dialog.setTitle(R.string.payment_late_dialog_title);

            TableLayout tableLayout = (TableLayout) dialog.findViewById(R.id.tableLayout);

            for(PaymentLate p : paymentLateArrayList) {
                TableRow tableRow = new TableRow(dialog.getContext());

                TextView paymentnumberText = new TextView(dialog.getContext());
                paymentnumberText.setText(p.getPaymentNumber());
                paymentnumberText.setPadding(5, 5, 5, 5);
                tableRow.addView(paymentnumberText);

                TextView emissionDateText = new TextView(dialog.getContext());
                emissionDateText.setText(p.getEmissionDate().substring(0, 9));
                emissionDateText.setPadding(5, 5, 5, 5);
                tableRow.addView(emissionDateText);

                TextView expireDateText = new TextView(dialog.getContext());
                expireDateText.setText(p.getExpiryDate().substring(0, 9));
                expireDateText.setPadding(5, 5, 5, 5);
                tableRow.addView(expireDateText);

                TextView valueText = new TextView(dialog.getContext());
                valueText.setText(p.getValue());
                valueText.setPadding(5, 5, 5, 5);
                tableRow.addView(valueText);

                TextView valueLateText = new TextView(dialog.getContext());
                valueLateText.setText(p.getValueDebit());
                valueLateText.setPadding(5, 5, 5, 5);
                tableRow.addView(valueLateText);

                tableLayout.addView(tableRow);
            }


            dialog.show();
        }
    }

    public interface ClientManagerInterface {
        public void onClientsListReturn(ArrayList<Client> clientsList);
        public void onEstablishmentsListReturn(ArrayList<Establishment> estabs);
        public void onDefaultEstablishmentReturn(Establishment establishment);
        public void onEstablishmentParamsReturn(EstabParamsDefault estabParamsDefault);
    }

    public interface ClientManagerParamsInterface {
        public void onClientPlacasReturn(ArrayList<Placa> placas);
        public void onClientAddressReturn(ArrayList<Address> addresses);
        public void onClientSetPlacaReturn(String id);
        public void onClientSetAddressReturn(String id);
    }


}
